package com.shop.service;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.JSON2.json.JSON;
import com.JSON2.json.JSONArray;
import com.JSON2.json.JSONObject;
import com.shop.daoImpl.GoodsDaoImpl;
import com.shop.daoImpl.MemberDaoImpl;
import com.shop.daoImpl.MerchantDaoImpl;
import com.shop.daoImpl.MerchantOrderDaoImpl;
import com.shop.daoImpl.OrderDaoImpl;
import com.shop.daoImpl.OrderGoodsListDaoImpl;
import com.shop.daoImpl.OrderLogisticsDaoImpl;
import com.shop.entity.Address;
import com.shop.entity.Goods;
import com.shop.entity.Member;
import com.shop.entity.Merchant;
import com.shop.entity.MerchantOrder;
import com.shop.entity.Order;
import com.shop.entity.OrderLogistics;
import com.shop.util.SettDetailA;

/**
 * 
 * <b>处理结算业务<b>
 * @author 威 
 * <br>2017年12月 
 *
 */
@Service
public class SettlementServiceImpl {
	@Resource 
	private OrderDaoImpl orderDaoImpl ;
	
	@Resource 
	private MemberDaoImpl memberDaoImpl ;
	
	@Resource
	private MerchantOrderDaoImpl merchantOrderDaoImpl ;
	
	@Resource
	private OrderLogisticsDaoImpl orderLogisticsDaoImpl ;
	
	@Resource
	private OrderGoodsListDaoImpl orderGoodsListDaoImpl ;
	
	@Resource
	private MerchantDaoImpl merchantDaoImpl ;
	
	@Resource 
	private GoodsDaoImpl goodsDaoImpl ;
	
	public void setOrderDaoImpl(OrderDaoImpl orderDaoImpl) {
		this.orderDaoImpl = orderDaoImpl;
	}
	public void setMemberDaoImpl(MemberDaoImpl memberDaoImpl) {
		this.memberDaoImpl = memberDaoImpl;
	}
	public void setMerchantOrderDaoImpl(MerchantOrderDaoImpl merchantOrderDaoImpl) {
		this.merchantOrderDaoImpl = merchantOrderDaoImpl;
	}
	public void setOrderLogisticsDaoImpl(OrderLogisticsDaoImpl orderLogisticsDaoImpl) {
		this.orderLogisticsDaoImpl = orderLogisticsDaoImpl;
	}
	public void setOrderGoodsListDaoImpl(OrderGoodsListDaoImpl orderGoodsListDaoImpl) {
		this.orderGoodsListDaoImpl = orderGoodsListDaoImpl;
	}
	public void setMerchantDaoImpl(MerchantDaoImpl merchantDaoImpl) {
		this.merchantDaoImpl = merchantDaoImpl;
	}
	public void setGoodsDaoImpl(GoodsDaoImpl goodsDaoImpl) {
		this.goodsDaoImpl = goodsDaoImpl;
	}
	
	public Session getSession(){
		ApplicationContext c = new ClassPathXmlApplicationContext("applicationContext.xml") ;
		SessionFactory sf = (SessionFactory) c.getBean("sessionFactory") ;
		return sf.openSession() ;
	}
	/**
	 * 
	 * 提交订单
	 * @see
	 * @param memberId
	 * @param goodsId
	 * void
	 *
	 */
	public void submitOrders(String data){
		System.out.println("submitOrders-2") ;
		//可以通过结算工具类计算金额以及优惠金额
		//获取会员id
		Session session = getSession() ;
		Member member = (Member) session.get(Member.class, 1) ;
		//System.out.println("输出memberid-" + member.getId()+",username-"+member.getUsername()+",password-"+member.getPassword()) ;
		
		//生成订单
		Order order = new Order() ;
		JSONObject object = (JSONObject) new JSON().parseObject(data) ;
		//System.out.println("submitOrders-4") ;
		
		//获取物流信息
		Integer memberAddres = (Integer) object.get("addresId") ;//获取用户地址id
		Integer nowAddressId = null ; //当前用户地址实体
		Set<Address> addrese = member.getAddress() ;
		for(Address address : addrese){
			//System.out.println("地址id："+address.getId() + "-" + memberAddres) ;
			if(address.getId() == memberAddres){
				nowAddressId = memberAddres ;
				//System.out.println("找到指定地址id") ;
			}
		}

		//获取所有商品信息
		JSONArray goodsArr = (JSONArray) object.get("orderList") ;
		List<JSONObject> lists = goodsArr.get() ;
		Integer len = lists.size() ;
		Integer goodsId = (Integer) lists.get(0).get("goodsId") ;
		//Integer merchantId = goodsDaoImpl.findById(goodsId).getMerchant().getId() ;
		Goods g = (Goods) session.get(Goods.class, goodsId) ;
		System.out.println("商品id："+g.getId()) ;
		Merchant mc = g.getMerchant() ;
		Integer merchantId = mc.getId() ;
		//System.out.println("商户id："+merchantId) ;
		
		SettDetailA sda = new SettDetailA(merchantId, lists.get(0)) ;
		//遍历每一个商品的JSON数据，并根据店铺下单
		for(int i = 1; i < len; i++){
			goodsId = (Integer) lists.get(i).get("goodsId") ;
			merchantId = ((Goods) session.get(Goods.class, goodsId)).getMerchant().getId() ;
			sda.startDo(merchantId, lists.get(i)) ;
		}
		
		//遍历完毕 （形象表示：接下来的操作是商户过来领各自的订单，并生成各自的物流表）
		//maps 中存储着商户id和订单
		Map<Integer, MerchantOrder> maps = sda.end() ;
		for(Entry<Integer, MerchantOrder> item : maps.entrySet()){
			MerchantOrder mOrder = item.getValue() ; 
			//System.out.println(item.getKey() + "-" + mOrder) ;
			Merchant merchant = (Merchant) session.get(Merchant.class, item.getKey()) ;
			
			OrderLogistics oLogist = new OrderLogistics() ;
			oLogist.setMemeberAddressId(nowAddressId) ;
			
			//System.out.println("商户订单开始生成物流") ;
			mOrder.setLogistics(oLogist) ;
			//System.out.println("mOrder" + mOrder) ;
			//System.out.println("商户订单生成物流完毕") ;
			
			//System.out.println(mOrder.getTotalAmount()) ;
			//System.out.println("商户认领订单");
			merchant.getMerchantOrder().add(mOrder) ;
			
			//System.out.println("总订单认领商户订单");
			order.getMerchantOrders().add(mOrder) ;
			
			//System.out.println("会员认领总订单") ;
			member.getOrder().add(order) ;
			session.beginTransaction() ;
			session.flush() ; //使用方法将所有瞬时态的数据转变成持久态
		}
		session.getTransaction().commit() ;
		session.close() ;
		//System.out.println("结束") ;
	}
	/**
	 * 
	 * 初始化结算页
	 * @see
	 * void
	 *
	 */
	public String initSettPage(){
		
		JSONObject jsonObject = new JSONObject() ;

		JSONArray jsonArray = new JSONArray() ;
		
		//商品1信息
		JSONObject jsonObject1 = new JSONObject() ; 
		jsonObject1.put("id", 1) ;
		jsonObject1.put("goodsId", 24) ;
		jsonObject1.put("img", "../image/test/tako.jpg") ;
		jsonObject1.put("discount", 0.88) ;
		jsonObject1.put("name", "tako小衣") ;
		jsonObject1.put("color", "绿色") ;
		jsonObject1.put("count", 1) ;
		jsonObject1.put("size", "L") ;
		jsonObject1.put("price", 189) ;
		jsonArray.add(jsonObject1) ;
		
		//商品2信息
		JSONObject jsonObject2 = new JSONObject() ; 
		jsonObject2.put("id", 2) ;
		jsonObject2.put("goodsId", 25) ;
		jsonObject2.put("img", "../image/test/tako1.jpg") ;
		jsonObject2.put("discount", 0.88) ;
		jsonObject2.put("name", "tako小衣") ;
		jsonObject2.put("color", "绿色") ;
		jsonObject2.put("count", 1) ;
		jsonObject2.put("size", "XL") ;
		jsonObject2.put("price", 188) ;
		jsonArray.add(jsonObject2) ;
		jsonObject.put("orderList", jsonArray) ;
		
		//地址信息
		JSONArray addresArr = new JSONArray() ;
		JSONObject addresObj = new JSONObject() ;
		addresObj.put("id", 1) ;
		addresObj.put("text", "广西省-北海市-银海区") ;
		addresArr.add(addresObj) ;
		jsonObject.put("address", addresArr) ;

		jsonObject.put("money", 377) ; //总金额
		return jsonObject.toString() ;
	}
}
